/**
 * <p>编辑的gridPanel控件，主要用于主从表维护页面的从表</p>
 * xtype = cceafdetaileditorgridpanel

 * @module Ext-ux-cceaf-control
 * @class cceaf.control.DetailEditorGridPanel
 * @extends Ext.grid.GridPanel
 * @since 1.0
 */
Ext.define('cceaf.control.DetailEditorGridPanel',{
	extend: 'Ext.grid.GridPanel', 
    alias: 'widget.cceafdetaileditorgridpanel',

    requires: [
       'Ext.data.Store'
    ],
            
    /**
	 * 列表model
	 *
     * @property model 
     * @type String
     * @default null
     */
	model: null,
    
	/**
	 * 操作区的toolbar，在组装后的toolbar后追加
	 *
     * @property operateToolbar 
     * @type toolbar
     * @default null
     */
	operateToolbar: null,
	
	/**
	 * 操作区的toolbar
	 *
     * @property reOperateToolbar 
     * @type attribute toolbar
     * @default null
     */
	reOperateToolbar: null,
	
	constructor: function(config){
		config = config || {};
		
		var store;
		if (!config.model) {
			//cceaf.util.ExtHelper.showInfoDialog('请设置子表的model！');
			alert('请设置子表的model！');
		} else{
			store = new Ext.data.Store({model: config.model});
		}
		
		if(config.local == 'remote'){
			store = new Ext.data.Store({
		    	proxy: {
		    		type: 'ajax',
		    		url:  config.queryAction,
		    		actionMethods: {  
			            read: 'POST'  
			        },  
		    		reader: {
						type: 'json',
						root: 'data',
						totalProperty: 'totalCount',
						messageProperty: 'message'
					}
				},
				
		        listeners: {
		        	'load': function(ds){
		        		var jr = ds.getProxy().getReader();
		        		if(!jr.jsonData) {
		        			cceaf.util.ExtHelper.showErrorDialog("没有设置正确的action或没有返回正确的json字符串！");
		        			return;
		        		}
		        		
		        		var message = jr.jsonData.message;
		        		var url = jr.jsonData.url;
		        		cceaf.util.ExtHelper.showInfoDialog(message,url);
		        	},
		        	'loadexception': function(ds){
		        		var jr = ds.getProxy().getReader();
		        		if(jr.jsonData){
			        		var message = jr.jsonData.message;
			        		var url = jr.jsonData.url;
			        		cceaf.util.ExtHelper.showErrorDialog(message,url);
		        		}
		        		else{
		        			cceaf.util.ExtHelper.showErrorDialog("返回无效的JSONDATA数据");
		        		}
		        	}
		        	
		        },
		        groupField: config.groupField,
		        autoDestroy: true,  
		        autoLoad:true,
		        //pageSize: 20,
		    	pageSize: szpshang.Global.PAGE_SIZE,
		    	model: config.model
		    });
		}
		
		var cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {
			pluginId: 'pluginIdOfcceafdetaileditorgridpanel',
            clicksToEdit: 1
        });
		
		/**
		 * 默认操作区
		 */
		var operateArea = [],i=0;
		operateArea[i] = {itemId: 'btnAddRow', iconCls: 'addIcon', text: ' 增加行 ', pressed: true, 
			handler: function(){
				var billGrid = this.up('gridpanel');
				var model = billGrid.model;
				if (!model) {
					cceaf.util.ExtHelper.showInfoDialog('请设置子表的model！');
					return;
				}
				
				var recordData = Ext.ModelManager.create({}, model);  
				
				billGrid.initAddRow(recordData);
				var count = billGrid.getStore().getCount();
				billGrid.getStore().insert(count, recordData);
				cellEditing.startEditByPosition({row:count, column:1});
			}
		};
		i++;
		operateArea[i] = '-';
		i++;
		
		operateArea[i] = {itemId: 'btnDeleteRow', iconCls: 'deleteIcon', text: ' 删除行 ', pressed: true, 
			handler: function(){
				//获取要删除对象的主键集
				var billGrid = this.up('gridpanel');
		    	var selModel = billGrid.getSelectionModel();
		    	var selectedCount = selModel.getCount();
		    	if(selectedCount == 0){
		    		cceaf.util.ExtHelper.showInfoDialog('请选择要删除的行');
		    		return;
		    	}
		    	else{
		    		//确定要删除对象
		    		Ext.MessageBox.show({
						title:'确认提示框',
						modal: true,
						msg: '确信要删除您选中的行吗？',
						minWidth: 200,
						buttons: Ext.MessageBox.YESNO,
						icon: Ext.MessageBox.QUESTION,
						fn:function(btn){
							if(btn == 'yes'){
								var selections = selModel.getSelection();
								cellEditing.cancelEdit(); 
								var store = billGrid.getStore();
								store.remove(selections);
								
								var records = store.getRange(0, store.getCount()); 
								store.removeAll();
								store.loadRecords(records);
							}
						}
					});
		    	}
			}
		};
		i++;
			
		var operateToolbar = config.operateToolbar;
		if(operateToolbar == null){
			var reOperateToolbar = config.reOperateToolbar;
			if (reOperateToolbar == null) {
				operateToolbar = {
					xtype: 'toolbar',
					items: operateArea
				};
			} else {
				operateToolbar = {
					xtype: 'toolbar',
					items: reOperateToolbar
				};
			}
		}else {
			var array = operateArea.concat(operateToolbar);
			operateToolbar = {
				xtype: 'toolbar',
				items: array
			};
		}
		
		//定义操作区
		var dockedItems =[];
		var pagingtoolbar = {
			xtype: 'cceaftoolbarpaging',
			store: store,
	        dock: 'bottom',  
	        displayInfo: true,
	        displayMsg:'当前显示 {0} - {1}条记录 /共 {2}条记录',
	      	emptyMsg:'无显示数据'
		};
			
		dockedItems[dockedItems.length] = operateToolbar;
		if (config.hasPagingToolbar === true) {
			dockedItems[dockedItems.length] = pagingtoolbar;
		}
			
		
		config = Ext.apply(config || {}, {
	        layout: 'anchor',
        	baseCls: 'x-panel'
		});
		
//		var height = config.height;
//		var width = config.width;
//		if(config.height == null){
//			height = cceaf.util.ExtHelper.getMainContentHeight();
//		}
//		if(config.width == null){
//			width = cceaf.util.ExtHelper.getMainContentWidth();
//		}
		
		Ext.applyIf(config || {}, {
			store: store,
			selModel: new Ext.selection.CheckboxModel(),
	        border: true,
	        frame: config.local != 'remote',
	        flex: 1,
	        autoScroll: true,
//	        height: height,
//	        width: width,
	        selType: 'cellmodel', // 选择类型设置为：单元格选择 
	        plugins: [cellEditing],
	        loadMask: {msg:'正在加载数据，请稍侯……'},
	        dockedItems: dockedItems,
	        listeners : {  
	            'selectionchange' : function(sm, selections) {  
	                this.down('#btnDeleteRow').setDisabled(selections.length == 0);  
	            }  
	        }
		});

		cceaf.control.DetailEditorGridPanel.superclass.constructor.apply(this, arguments);
	},
	
	viewConfig: {
		forceFit:true,
		enableRowBody:true,
		getRowClass : function(record, rowIndex, p, ds){
			return '';
		}
	},
	
	initComponent : function(){
		cceaf.control.DetailEditorGridPanel.superclass.initComponent.call(this);
    },

    initEvents: function() {
    	cceaf.control.DetailEditorGridPanel.superclass.initEvents.call(this);
    },
    
    /**
     * 在点击增加行时执行,父类不执行,可在子类重载
     */
    initAddRow: function(){
    	
    }
    
});